package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.R;
import com.atguigu.config.exceptionhandler.GuliException;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstantVodUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(description = "视频模块")
@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {

    @Autowired
    private VodService vodService;

    @ApiOperation(value = "上传视频")
    @PostMapping("uploadAlyVideo")
    public R uploadAliVideo(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String videoId = vodService.uploadVideo(file);
        return R.ok().data("videoID",videoId).data("videoName",fileName);
    }

    @ApiOperation(value = "删除视频")
    @DeleteMapping("removeAlyVideo/{vId}")
    public R removeAlyVideo(@PathVariable String vId) {
        try {
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频的request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置对象
            request.setVideoIds(vId);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw  new GuliException(20001,"删除视频失败");
        }
    }

    @ApiOperation(value = "删除多个视频")
    @DeleteMapping("removeBatch")
    public R deleteBatch(@RequestParam("vIdList") List<String> vIdList) {

        vodService.removeMoreAlyVideo(vIdList);
        return R.ok();
    }


    @ApiOperation(value = "获取视频播放凭证")
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id) {

        try {
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取视频凭证request、response
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
            //向request对象里面设置视频id
            request.setVideoId(id);
            //调用初始化对象里面的方法传递request,获取数据
            response = client.getAcsResponse(request);
            //输出凭证
            String playAuth = response.getPlayAuth();


            return R.ok().data("playAuth",playAuth);

        } catch(Exception e) {
            throw new GuliException(20001,"获取凭证失败");

        }

    }


}
